package com.yj.ccs.abs.common.mapper;

import com.yj.ccs.abs.common.entity.Area;
import com.yj.ccs.abs.common.entity.City;
import com.yj.ccs.abs.common.entity.Province;
import com.yj.ccs.abs.common.entity.Region;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Copyright 2018 (C) Yunjian-VC
 * Created on : 2018/3/15
 * Author     : Unbelievable9
 **/
@Repository
@Mapper
public interface RegionMapper {

    @Select("SELECT * FROM area WHERE `code` = #{code} LIMIT 1")
    Area getAreaByCode(@Param("code") String code);

    @Select("SELECT * FROM province")
    List<Province> getAllProvinces();

    @Select("SELECT * FROM city WHERE provinceCode = #{code}")
    List<City> getAllCitiesByProvinceCode(@Param("code") String code);

    @Select("SELECT * FROM area WHERE cityCode = #{code}")
    List<Area> getAllAreasByCityCode(@Param("code") String code);

    @Select("SELECT p.`name` AS province_name, c.`name` AS city_name, a.`name` AS area_name " +
            "FROM area a " +
            "LEFT JOIN city c ON ( a.cityCode = c.`code` ) " +
            "LEFT JOIN province p ON ( a.provinceCode = p.`code` ) " +
            "WHERE a.`code` = #{code} LIMIT 1")
    Region getRegionByAreaCode(@Param("code") String code);
}
